package pro.lepers.portfolio.view.contents.portfolio.data.contents {
	import fr.digitas.flowearth.core.IIterator;
	import fr.digitas.flowearth.event.NodeEvent;
	import fr.digitas.flowearth.mvc.address.structs.INode;

	import pro.lepers.portfolio.view.contents.portfolio.data.ProjectDatas;

	import flash.utils.Dictionary;

	/**
	 * @author Pierre Lepers
	 */
	public class ProjectContentProvider {

		
		
		public function ProjectContentProvider( plist : Vector.<AbstractContentData> ) {
			
			_compile( plist );
		}
	
		public function get length () : int {
			return _aContents.length;
		}
		
		
		public function dispose () : void {
			for (var i : int = 0; i < _aContents.length; i++) {
				_aContents[ i ].dispose();
				_aContents[ i ] = null;
			}
			_aContents = null;
			_dContents = null;
			
			rootNode.removeEventListener( NodeEvent.CHILD_CHANGE , onChildChange, true );
			_project = null;
		}

		public function getContents() : IIterator {
			return new ContentIterator( _aContents );
		}
		
		public function getCurrentContent() : AbstractContentData {
			if( ! rootNode.getCurrentChild() ) return null;
			return _dContents[ rootNode.getCurrentChild().getId() ];
		}
		

		public function next() : void {
			if( ! rootNode.getCurrentChild() )
				rootNode.getChild( _lastChildId ).activate();
			else {
				var index : int = _aContents.indexOf( getCurrentContent() );
				if( index < _aContents.length-1 )
					rootNode.getChild( _aContents[ index + 1 ]._nId ) .activate();
			}
		}
		
		public function prev() : void {
			if( ! rootNode.getCurrentChild() ) return;
			var index : int = _aContents.indexOf( getCurrentContent() );
			if( index > 0 )
				rootNode.getChild( _aContents[ index - 1 ]._nId ) .activate();
			else
				rootNode.activate();
		}
		
		
		public function get rootNode() : INode {
			return _project.node;
		}
		
		
		public function setProject(project : ProjectDatas) : void {
			_project = project;
			rootNode.addEventListener( NodeEvent.CHILD_CHANGE , onChildChange );
			onChildChange( null );
		}
		
		private function onChildChange(event : NodeEvent) : void {
			if( rootNode.getCurrentChild() )
				_lastChildId = rootNode.getCurrentChild().getId();
			else if( _lastChildId == null )
				_lastChildId = rootNode.getChilds().next().getId();
		}

		private var _project : ProjectDatas;
		
		private var _lastChildId : String;
		
		
		private function _compile( plist : Vector.<AbstractContentData> ) : void {
			_aContents = new Vector.<AbstractContentData>( plist.length , true );
			_dContents = new Dictionary();

			for (var i : int = 0; i < plist.length ; i ++) {
				_aContents[ i ] = plist[ i ];
				_aContents[ i ]._nId = i.toString();
				_dContents[ _aContents[ i ]._nId ] = _aContents[ i ];
			}
		}

		private var _aContents : Vector.<AbstractContentData>;
		
		private var _dContents : Dictionary;

	}
}

import fr.digitas.flowearth.core.IIterator;

import pro.lepers.portfolio.view.contents.portfolio.data.contents.AbstractContentData;

class ContentIterator implements IIterator {

	public function ContentIterator( a : Vector.<AbstractContentData> ) {
		_a = a;
		_c = 0;
	}

	public function next() : Object {
		return _a[_c ++];
	}

	public function hasNext() : Boolean {
		return _a.length > _c;
	}

	private var _a : Vector.<AbstractContentData>;
	private var _c : int;
}


